require(['./config'], () => {
    require(['jquery', 'artTemplate', 'loadHeaderFooter'], ($, template) => {
        class Detail {
            constructor() {
                this.init()
                if (this.cart) {
                    this.totalmoney()
                    this.checkschange()
                    this.setAllCheck()
                }
            }
            init() {
                const cart = localStorage.getItem('cart')
                if (cart) {
                    this.cart = JSON.parse(cart)
                    $('#pricelist').html(
                        template('paymentTemplate', { cart: this.cart })
                    )

                } else {
                    $('#empty').show()
                    $('#exist').hide()
                }
            }
            checkschange() {
                const _this = this
                $('.second').on('change', function () {
                    const id = $(this).data('id')
                    _this.cart = _this.cart.map(shop => {
                        if (shop.id === id) {
                            shop.check = $(this).prop('checked')
                        }
                        return shop
                    })
                    //修改好的 重新存
                    localStorage.setItem('cart', JSON.stringify(_this.cart))
                    _this.totalmoney()
                    _this.setAllCheck()
                })
            }
            setAllCheck() {
                console.log(123);
                $('#all-check').prop('checked', this.cart.every(shop => shop.check))
            }
            totalmoney() {
                const total = this.cart.reduce((money, shop) => {
                    if (shop.check) {
                        money += shop.price * shop.count
                    }
                    return money
                }, 0)
                $('#totalone').html(total.toFixed(2))
                $('#totaltwo').html(total.toFixed(2))
            }
        }
        new Detail()
    })
})